import BaseControlTemplate from "../components/UseControl/UseControlTemplate";
import { deepClone } from "@/utils";
export default {
  components: { BaseControlTemplate },
  props: {
    params: {
      type: Object,
      default: () => {},
    },
    pageViewMode: {
      type: String,
      default: "noraml",
    },
    isCreate: {
      type: Boolean,
      default: false,
    },
    readonly: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      modelValue2: "",
      validateError: false,
    };
  },
  created() {
    this.newParams = deepClone(this.params);
  },
  computed: {
    modelValue: {
      get() {
        if (this.newParams.controlType === "DatePicker") {
          return "";
        }
        switch (this.pageViewMode) {
          case "design":
            this.modelValue2 = this.newParams.defaultValue;
            break;
          case "runtime":
            this.modelValue2 = this.isCreate
              ? this.newParams.defaultValue
              : this.newParams.value;
            break;
          case "print":
            this.modelValue2 = this.newParams.value;
            break;
          default:
            break;
        }
        return "";
      },
      set(newVal) {
        this.modelValue2 = newVal;
      },
    },
  },
  methods: {
    validateRequired() {
      if (
        this.pageViewMode !== "runtime" ||
        this.readonly ||
        !this.newParams.required
      ) {
        return;
      }
      this.validateError = ["Select", "MultipleSelect", "Checkbox"].includes(
        this.newParams.controlType
      )
        ? this.modelValue2.length === 0
        : !this.modelValue2;
    },
    saveDialogConfig(val) {
      for (var i in val) {
        Object.prototype.hasOwnProperty.call(this.newParams, i) &&
          (this.newParams[i] = val[i]);
      }
      if (this.newParams.controlType === "Table") {
        this.$emit("tagValue2Select", {
          id: this.newParams.id,
          title: this.newParams.label,
          data: {
            type: "setTitle",
          },
        });
      }
    },
    validate() {
      if (this.pageViewMode === "runtime") {
        this.validateRequired();
        this.newParams.value = this.modelValue2;
      }
      return {
        validateError: this.validateError,
        controlValue: this.newParams,
      };
    },
  },
};
